<template>
  <div class="app-container">
    <!-- <PageTitle title="订单列表" ref="pageTitle" v-model="listQuery.city" @onChange="changeCity" /> -->
    <div class="filter-container">
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-plus"
        @click="$refs.edit.handleCreate()"
        >新增机柜</el-button
      >
    </div>
    <div class="filter-container">
      <el-input
        style="width: 160px"
        class="filter-item"
        placeholder="请输入景区名称"
        v-model="listQuery.attraction_name"
      ></el-input>
      <el-input
        style="width: 160px"
        class="filter-item"
        placeholder="请输入机柜编号"
        v-model="listQuery.number"
      ></el-input>
      <el-select
        clearable
        collapse-tags
        style="width: 160px"
        class="filter-item"
        v-model="listQuery.status"
        placeholder="请选择机柜状态"
      >
        <el-option
          v-for="item in statusOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>

      <el-button class="filter-item" size="mini" plain @click="handleReset">重置</el-button>
      <el-button class="filter-item" size="mini" type="primary" @click="handleFilter"
        >查询</el-button
      >
    </div>
    <Table :data="list" :columns="columns" :loading="listLoading">
      <template v-slot:status>
        <el-table-column label="机柜状态" width="80" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.status === 'online'">在线</span>
            <span v-if="scope.row.status === 'offline'" style="color: #dc4a4a">离线</span>
          </template>
        </el-table-column>
      </template>
      <template v-slot:is_open_eject>
        <el-table-column label="弹出工具" width="80" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.is_open_eject" style="color: #2aa23e">已开通</span>
            <span v-else>未开通</span>
          </template>
        </el-table-column>
      </template>

      <template v-slot:action>
        <el-table-column label="操作" minWidth="160" fixed="right">
          <template slot-scope="scope">
            <el-tooltip class="item" effect="dark" content="编辑" placement="top">
              <i class="el-icon-edit table-action" @click="$refs.edit.handleEdit(scope.row.id)"></i>
            </el-tooltip>
            <el-tooltip class="item" effect="dark" content="管理" placement="top">
              <i
                class="el-icon-setting table-action"
                @click="$refs.set.handleDeviceList(scope.row.id, scope.row.number)"
              ></i>
            </el-tooltip>
          </template>
        </el-table-column>
      </template>
    </Table>
    <div class="pagination-container">
      <pagination
        :total="count"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.size"
        @pagination="fetchData"
      />
    </div>
    <Edit ref="edit" />
    <SetDevice ref="set" />
  </div>
</template>

<script>
import { getList } from '@/api/cabinets'
import Edit from './components/Edit'
import SetDevice from './components/SetDevice'
export default {
  name: 'CabinetsIndex',
  components: { Edit, SetDevice },
  data() {
    return {
      list: null,
      count: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        size: 10,
        attraction_name: '',
        number: '',
        status: ''
      },
      statusOptions: [
        {
          value: 'online',
          label: '在线'
        },
        {
          value: 'offline',
          label: '离线'
        }
      ],
      columns: [
        {
          type: 'index',
          align: 'center',
          label: '序号',
          width: '50'
        },
        {
          label: '景点名称',
          width: '160',
          prop: 'attraction_name',
          showOverflowTooltip: true
        },
        {
          label: '机柜编号',
          width: '120',
          prop: 'number',
          align: 'center'
        },
        {
          label: '微信商户ID',
          width: '120',
          prop: 'wx_mch_id',
          align: 'center'
        },

        {
          label: '机柜位置',
          width: '100',
          prop: 'location',
          align: 'center'
        },
        {
          label: '讲解器数量',
          width: '120',
          prop: 'device_num',
          align: 'center'
        },
        {
          label: '可租借数量',
          width: '120',
          prop: 'free_device_num',
          align: 'center'
        },
        {
          label: '机柜状态',
          slot: 'status'
        },
        {
          label: '弹出工具',
          slot: 'is_open_eject'
        },

        {
          label: '操作',
          slot: 'action'
        }
      ]
    }
  },

  created() {
    this.fetchData()
  },
  mounted() {},
  methods: {
    // 获取数据
    fetchData() {
      this.listLoading = true
      getList(this.listQuery).then((res) => {
        this.list = res.data.list_data
        this.count = res.data.paging.count
        this.listLoading = false
      })
    },

    // 筛选
    handleFilter() {
      this.listQuery.page = 1
      this.fetchData()
    },
    /**
     * @description:
     * @return {*}
     */
    handleReset() {
      this.listQuery = {
        page: 1,
        size: 10,
        attraction_name: '',
        number: '',
        status: ''
      }
      this.fetchData()
    },
    /**
     * @description: 订单详情
     * @param {*} sn
     * @return {*}
     */
    handleDetail(sn, can_consume) {
      this.$router.push({ path: `/orders/detail/${sn}`, query: { can_consume } })
    }
  }
}
</script>
<style scoped>
.filter-container {
  justify-content: flex-start !important;
  flex-wrap: wrap;
}
.el-button {
  margin-bottom: 5px;
  margin-left: 0;
  margin-right: 5px;
}
</style>
